package com.ldj.hibernate;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;

import com.ldj.struts.form.NoticeInfoForm;

public class NoticeInfoDAO extends BaseHibernateDAO {
	public NoticeInfoDAO(HttpServletRequest request, HttpServletResponse response) {
		super(request, response);
		// TODO Auto-generated constructor stub
	}

	private static final Log log = LogFactory.getLog(NoticeInfoDAO.class);
	
	public List findAllPage(String startDate, String endDate, NoticeInfoForm nForm) {
		try {
			String queryString = "from NoticeInfo where noticeInfoDelOper='0' ";
			if(nForm != null){	
				if(nForm.getNoticeInfoTitle() != null && !"".equals(nForm.getNoticeInfoTitle())){
					queryString += " and noticeInfoTitle like '%" + nForm.getNoticeInfoTitle() + "%'";
				}
				if(nForm.getNoticeInfoType() != null && !"".equals(nForm.getNoticeInfoType())){
					queryString += " and noticeInfoType like '%" + nForm.getNoticeInfoType()+ "%'";
				}
				if((startDate != null && !"".equals(startDate))){
					queryString += " and noticeInfoDate >= to_date('" + startDate + "','yyyy-mm-dd')";
				}
				if((endDate != null && !"".equals(endDate))){
					queryString += " and noticeInfoDate <= to_date('" + endDate + " 23:59:59','yyyy-mm-dd hh24:mi:ss')";
				}
			}
			queryString += "order by noticeInfoDate desc";
			Query queryObject = getSession().createQuery(queryString);
			this.setPageSize(8);
			return this.jlist(queryObject);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public NoticeInfo findById(java.lang.String id) {
		log.debug("getting NoticeInfo instance with id: " + id);
		try {
			NoticeInfo instance = (NoticeInfo) getSession().get(
					"com.ldj.hibernate.NoticeInfo", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	public void save(NoticeInfo transientInstance) {
		log.debug("saving NoticeInfo instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}
	
	public NoticeInfo merge(NoticeInfo detachedInstance) {
		try {
			NoticeInfo result = (NoticeInfo) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}
	
	public void delete(NoticeInfo persistentInstance) {
		log.debug("deleting NoticeInfo instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}
}